using KinonekoSoftware.Studio.Pages.Galleries.Filters;
using KinonekoSoftware.Studio.Services;

namespace KinonekoSoftware.Studio.Pages.Galleries
{
    partial class InstanceGalleryViewModel2
    {
        private GalleryFilter GetFilter()
        {
            if (_filter is GalleryFilter gf)
            {
                return gf;
            }

            var a = Filters.OfType<GalleryFilter>()
                           .FirstOrDefault();

            //
            //
            return a ?? GalleryScraffolds._Filters[0];
        }

        internal void OnFilterChanged()
        {
            //
            // 重置数据源。
            ReloadDataSources();
        }

        private void OnFilterChanged(IGalleryFilter oldValue, IGalleryFilter newValue)
        {
            if (oldValue is GalleryFilter gf)
            {
                gf.SetParent(null);
            }

            if (newValue is GalleryFilter filter)
            {
                filter.SetParent(this);
            }

            if(_filter is not null && newValue is not null && _filter.Equals(newValue))
            {
                return;
            }

            if(newValue is null)
            {
                return;
            }

            //
            // 重置数据源
            ReloadDataSources();
        }
        
        public IGalleryFilter Filter
        {
            get => _filter;
            set
            {
                var oldValue = _filter;

                OnFilterChanged(oldValue, value);
                SetValue(ref _filter, value);

            }
        }

    }
}